﻿using System;
using System.Drawing;
using System.Windows.Forms;
using System.Xml.Serialization;
using SalaryReport.Core.Events;

namespace SalaryReport.Core.Excel
{
    [Serializable]
    [XmlInclude(typeof(ExcelColumn))]
    [XmlInclude(typeof(ExcelCell))]
    public abstract class ExcelItem : INotify
    {
        private const int topMargin = 10;
        protected const int panelHeight = 27;
        protected const int boxSize = 150;
        protected const int btnSize = 75;
        protected const int boxNumSize = 50;
        protected const int margin = 2;

        protected readonly Panel container;
        private Control parent;
        private readonly TextBox txtName;
        private readonly TextBox txtTemplateName;
        public readonly Button btnRemove;

        public string Name 
        { 
            get { return txtName.Text; }
            set { txtName.Text = value; }
        }

        public string Value { get; set; }

        public string TemplateName
        {
            get { return txtTemplateName.Text; }
            set { txtTemplateName.Text = value; }
        }

        public bool IsMandatory { get; set; }

        public event EventHandler<SalaryArgs> OnNotify;

        public void Notify(SalaryArgs args)
        {
            if (OnNotify != null)
            {
                OnNotify(this, args);
            }
        }

        protected ExcelItem()
        {
            container = new Panel { Height = panelHeight };
            btnRemove = new Button {Text = "Удалить", Width = btnSize};
            btnRemove.Click += btnRemove_Click;
            txtName = new TextBox { Width = boxSize, Location = new Point(margin * 2 + btnSize, margin) };
            txtTemplateName = new TextBox { Width = boxSize, Location = new Point(boxSize + btnSize + margin * 3, margin) };
            container.Controls.Add(btnRemove);
            container.Controls.Add(txtName);
            container.Controls.Add(txtTemplateName);
        }

        void btnRemove_Click(object sender, EventArgs e)
        {
            parent.Controls.Remove(container);
        }

        public virtual void DisplayOnElement(Control element, int index)
        {
            parent = element;
            container.Width = element.Width;
            container.Location = new Point(0, panelHeight * index + topMargin);
            element.Controls.Add(container);
        }
    }
}